<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>优惠券配置列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/public/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/public/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">

                <table class="layui-hide" id="test-table-reload" lay-filter="test-table-reload"></table>

                <script type="text/html" id="test-table-reload-toolbar">
                    <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle-fine"></i>添加优惠券配置</button>
                </script>

                <script type="text/html" id="test-table-reload-barDemo">
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                </script>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="merchantId" value="${merchantId}">
<input type="hidden" id="merchantName" value="${merchantName}">
<script src="/public/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/public/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'utils'], function () {
        var $ = layui.$,
                table = layui.table,
                admin = layui.admin;

        //方法级渲染
        table.render({
            elem: '#test-table-reload'
            , url: "/merchant/getMerchantCouponList?merchantId=" + $("#merchantId").val()
            , toolbar: '#test-table-reload-toolbar'
            , defaultToolbar: ['filter']
            , cellMinWidth: 80
            , cols: [[
                {field: 'couponName', title: '优惠券名称', fixed: true}
                , {field: 'invalid', title: '展示状态', templet: invalid}
            ]]
            , page: true
            //, height: 520
        });

        //头工具栏事件
        table.on('toolbar(test-table-reload)', function (obj) {
            if (obj.event === 'add') {
                window.location.href = "/merchant/addMerchantCoupon?merchantId="+ $("#merchantId").val() + "&merchantName=" + $("#merchantName").val();
            }
        });

        //监听行工具事件
        table.on('tool(test-table-reload)', function (obj) {
            var data = obj.data;
            if (obj.event === 'show') {
                layer.confirm('现在的优惠券是显示状态，是否更改为隐藏状态？', {icon: 7, title: '提示', skin: 'layui-layer-molv', anim: 4}, function () {
                    $.get("/merchant/updateCouponInValid?id=" + data.id + "&invalid=1", function (result) {
                        if (result.code > 0) {
                            layer.msg(result.msg, {icon: 2});
                        } else {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                window.location.reload();
                            });
                        }
                    });
                });
            } else if (obj.event === 'hide') {
                layer.confirm('现在的优惠券是隐藏状态，是否更改为显示状态？', {icon: 7, title: '提示', skin: 'layui-layer-molv', anim: 4}, function () {
                    $.get("/merchant/updateCouponInValid?id=" + data.id + "&invalid=0", function (result) {
                        if (result.code > 0) {
                            layer.msg(result.msg, {icon: 2});
                        } else {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                window.location.reload();
                            });
                        }
                    });
                });
            }
        });

        function invalid(data) {
            if(data.invalid === 0){
                return "<div class='layui-btn layui-btn-xs' lay-event='show'>"+ "显示"+"</div>";
            }
            if(data.invalid === 1){
                return "<div class='layui-btn layui-btn-xs' lay-event='hide'>"+ "隐藏"+"</div>";
            }
        }

    });
</script>
</body>
</html>